package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.entity.ImgReq;
import com.easy.dfcapi.res.*;
import com.easy.dfcapi.service.ApiUploadService;
import com.easy.dfcapi.service.ApiStoreService;
import com.easy.dfcapi.service.ApiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

/**
 * 店铺相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/store")
public class ApiStoreController extends Api20Controller {


    @Autowired
    private ApiUserService userService;

    @Autowired
    private ApiStoreService storeService;

    @Autowired
    private ApiUploadService imageService;

    /**
     * 店铺申请
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "StoreAdd", method = RequestMethod.POST)
    public Result StoreAdd(Response<StoreRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.StoreAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺列表
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "StoreList", method = RequestMethod.POST)
    public Result StoreList(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.StoreList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺信息
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "StoreInfo", method = RequestMethod.POST)
    public Result StoreInfo(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.StoreInfo(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺详情
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "StoreDetail", method = RequestMethod.POST)
    public Result StoreDetail(Response<UserRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.StoreDetail(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺LOGO
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "StoreAvatar", method = RequestMethod.POST)
    public Result StoreAvatar(Response<StoreRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            List<ImgReq> imgList = imageService.UploadImg(request);
            return storeService.StoreAvatar(imgList, response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺招牌
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "StoreImg", method = RequestMethod.POST)
    public Result StoreImg(Response<StoreRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            List<ImgReq> imgList = imageService.UploadImg(request);
            return storeService.StoreImg(imgList, response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }


    /**
     * 店铺升级
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "Upgrade", method = RequestMethod.POST)
    public Result Upgrade(Response<StoreRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.Upgrade(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺升级缴费
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpgradePay", method = RequestMethod.POST)
    public Result UpgradePay(Response<PayInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.UpgradePay(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺升级查询
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UpgradeQuery", method = RequestMethod.POST)
    public Result UpgradeQuery(Response<StoreRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.UpgradeQuery(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 支付完成回调
     * @param aliNotify
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AliNotify")
    public String AliNotify(AliNotifyRes aliNotify, HttpServletRequest request, Model model) {
        try {
            //boolean signVerified = SignUtils.rsaCheckV1(paramsMap, ALIPAY_PUBLIC_KEY, "UTF-8"); //调用SDK验证签名
            //if(signVerfied){
            //  验签成功后
            //按照支付结果异步通知中的描述，对支付结果中的业务内容进行1\2\3\4二次校验，校验成功后在response中返回success，校验失败返回failure
            //}else{
            //  验签失败则记录异常日志，并在response中返回failure.
            //}
            Enumeration enu=request.getParameterNames();
            while(true){
                if (!(enu.hasMoreElements())) break;
                String paraName=(String)enu.nextElement();
                System.out.println(paraName+": "+request.getParameter(paraName));
                logger.error("ALIpay:"+ paraName+" : "+request.getParameter(paraName));
            }
            return storeService.AliNotify(aliNotify);
        } catch (Exception e) {
            logger.error(e.toString());
            return "fail";
        }
    }

    /**
     * 店铺首页
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "HomePage", method = RequestMethod.POST)
    public Result HomePage(Response<StoreInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.HomePage(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺全部宝贝
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AllProductList", method = RequestMethod.POST)
    public Result AllProductList(Response<StoreInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.AllProductList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺新品上新Banner
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "NewProductBanner", method = RequestMethod.POST)
    public Result NewProductBanner(Response<StoreInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.NewProductBanner(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺新品上新产品列表
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "NewProductList", method = RequestMethod.POST)
    public Result NewProductList(Response<StoreInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.NewProductList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺视频点播
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ViewVideo", method = RequestMethod.POST)
    public Result ViewVideo(Response<StoreInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.ViewVideo(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 店铺视频直播
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ViewLive", method = RequestMethod.POST)
    public Result ViewLive(Response<StoreInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return storeService.ViewLive(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
}
